﻿using System;
using System.Data;
using eTMS.Controllers.MonThiService;
using eTMS.Controllers.MonHocService;
using System.Collections.Generic;
using eTMS.Controllers.PhongThiService;
namespace eTMS.Controllers
{
    public class MonThiController : MonThiControllerBase
    {
        public MonThiController()
            : base()
        {
        }
        private DataTable _tblMonThiRemoved;
        private DataTable _tblThiSinh;
        private DataTable _tblThiSinhRemoved;
        private List<PhongThiService.PhongThiEntity> _lsPhongThi = new List<eTMS.Controllers.PhongThiService.PhongThiEntity>();
        public List<PhongThiService.PhongThiEntity> LsPhongThi
        {
            get
            {
                return _lsPhongThi;
            }
        }
        public DataTable tblThiSinh
        {
            set
            {
                _tblThiSinh = value;
            }
            get
            {
                return _tblThiSinh;
            }
        }
        DanhSachCamThiController danhSachCamThiController;
        public void LoadAllDSThiSinh(MonThiEntity oMonThi, int HocKy_ID, int NamHoc_ID, bool IsHienThiCamThi)
        {
            ThiSinhMonThiService.ThiSinhMonThiServiceClient thiSinhMonThiServiceClient = new eTMS.Controllers.ThiSinhMonThiService.ThiSinhMonThiServiceClient();
            _tblThiSinh = thiSinhMonThiServiceClient.SelectAllDSWithThiSinhByMonThi_ID(oMonThi.MonThi_ID).Tables[0];
            SinhVienController.AddTenGioiTinh(_tblThiSinh);
            if (IsHienThiCamThi)
            {
                if (danhSachCamThiController == null)
                    danhSachCamThiController = new DanhSachCamThiController();
                danhSachCamThiController.LoadAllDSDanhSachCamThi(oMonThi.MonHoc_ID, HocKy_ID, NamHoc_ID);
                _tblThiSinh.Columns.Add("IsCamThi", typeof(bool));
                foreach (DataRow r in _tblThiSinh.Rows)
                {
                    r["IsCamThi"] = danhSachCamThiController.Table.Select("SinhVien_ID=" + r["SinhVien_ID"].ToString()).Length > 0;
                }
            }
            _tblThiSinh = SortByLastName("TenSinhVien", "Ho", "Ten", _tblThiSinh);
            AddIndexToTable(_tblThiSinh);
            _tblThiSinhRemoved = _tblThiSinh.Clone();

        }
        public void LoadAllDSThiSinh()
        {
            LoadAllDSThiSinh(MonThiObject, -1, -1, false);
        }
        public void LoadMonThiByKyThi(int KyThi_ID)
        {
            Table = dao.SelectAllDSWithMonHoc(KyThi_ID).Tables[0];
            AddIndexToTable();
        }
        public List<int> GetListMonHocID()
        {
            List<int> ls = new List<int>();
            if (Table == null)
            {
                return ls;
            }
            foreach (DataRow r in Table.Rows)
            {
                ls.Add((int)r["MonHoc_ID"]);
            }
            return ls;
        }
        public void AddMonThi(List<MonHocEntity> listMonHoc)
        {
            if (_tblMonThiRemoved == null)
                _tblMonThiRemoved = Table.Clone();
            foreach (MonHocEntity monHocEntity in listMonHoc)
            {
                DataRow row = null;
                DataRow[] rows = _tblMonThiRemoved.Select("MonHoc_ID=" + monHocEntity.MonHoc_ID.ToString());
                if (rows.Length == 0)
                {
                    row = Table.NewRow();
                    row["MaMonHoc"] = monHocEntity.MaMonHoc;
                    row["TenMonHoc"] = monHocEntity.TenMonHoc;
                    row["MonHoc_ID"] = monHocEntity.MonHoc_ID;
                    Table.Rows.Add(row);
                }
                else
                {
                    row = rows[0];
                    Table.ImportRow(row);
                    _tblMonThiRemoved.Rows.Remove(row);
                }
            }
        }
        public void RemoveMonThi(List<DataRow> listMonHoc)
        {
            if (_tblMonThiRemoved == null)
                _tblMonThiRemoved = Table.Clone();
            foreach (DataRow monHocRow in listMonHoc)
            {
                _tblMonThiRemoved.ImportRow(monHocRow);
                Table.Rows.Remove(monHocRow);
            }
        }
        public void AddSinhVienToMonThi(int MonThi_ID, List<DataRow> lsSinhVienRow)
        {
            if (_tblThiSinhRemoved == null) _tblThiSinhRemoved = tblThiSinh.Clone();
            foreach (DataRow r in lsSinhVienRow)
            {
                DataRow[] rows = _tblThiSinhRemoved.Select("SinhVien_ID=" + r["SinhVien_ID"].ToString() + " AND MonThi_ID=" + MonThi_ID.ToString());
                if (rows.Length == 0)
                {
                    DataRow nr = _tblThiSinh.NewRow();
                    foreach (DataColumn col in _tblThiSinh.Columns)
                    {
                        if (r.Table.Columns.Contains(col.ColumnName))
                        {
                            nr[col.ColumnName] = r[col.ColumnName];
                        }
                    }
                    nr["MonThi_ID"] = MonThi_ID;
                    if (_tblThiSinh.Columns.Contains("IsCamThi"))
                    {
                        nr["IsCamThi"] = danhSachCamThiController.Table.Select("SinhVien_ID=" + r["SinhVien_ID"].ToString()).Length > 0;
                    }
                    _tblThiSinh.Rows.Add(nr);
                }
                else
                {
                    _tblThiSinh.ImportRow(rows[0]);
                    _tblThiSinhRemoved.Rows.Remove(rows[0]);
                }
            }
            AddIndexToTable(_tblThiSinh);
        }
        public void RemoveSinhVienFromMonThi(List<DataRow> lsSinhVienRow)
        {
            if (_tblThiSinhRemoved == null) _tblThiSinhRemoved = tblThiSinh.Clone();
            foreach (DataRow r in lsSinhVienRow)
            {
                _tblThiSinhRemoved.ImportRow(r);
                _tblThiSinh.Rows.Remove(r);
            }
        }
        public void LoadAllPhongThi(int MonThi_ID)
        {
            PhongThiService.PhongThiServiceClient phongThiService = new eTMS.Controllers.PhongThiService.PhongThiServiceClient();
            _lsPhongThi = new List<eTMS.Controllers.PhongThiService.PhongThiEntity>(phongThiService.SelectAllFilter("MonThi_ID="+MonThi_ID.ToString()));

        }
        public void SaveMonThi(int KyThi_ID)
        {

            if (_tblMonThiRemoved != null)
            {
                foreach (DataRow row in _tblMonThiRemoved.Rows)
                {
                    if (!row.IsNull("MonThi_ID"))
                    {
                        dao.DeleteByID(ConvertInt(row["MonThi_ID"]));
                    }
                }
            }
            foreach (DataRow row in Table.Rows)
            {
                MonThiObject = MapDataRowToObject(row);
                MonThiObject.KyThi_ID = KyThi_ID;                
                MonThiObject.TrangThai = 0;
                if (MonThiObject.IsNullNgayThi || MonThiObject.NgayThi == DateTime.MinValue)
                {
                    MonThiObject.NgayThi = DateTime.Now;
                }
                if (MonThiObject.MonThi_ID > 0)
                {
                    dao.Update(MonThiObject);
                }
                else
                {
                    dao.Save(MonThiObject);
                }
            }
        }
        public void UpdateSBD(int Start, int Len)
        {
            int Count = Start;
            foreach (DataRowView r in _tblThiSinh.DefaultView)
            {
                string sbd = Commons.Common.FixLength(Count.ToString(), Len);
                r["SoBaoDanh"] = sbd;
                Count++;
            }
        }
        public void SaveThiSinhMonThi(bool IsBoSVCamThi)
        {
            DeleteThiSinhMonThi();
            string Filter = "";
            if (IsBoSVCamThi)
                Filter = "NOT IsCamThi";
            SaveThiSinhMonThiRows(_tblThiSinh.Select(Filter));
        }
        private void DeleteThiSinhMonThi()
        {
            if (_tblThiSinhRemoved == null) return;
            ThiSinhMonThiService.ThiSinhMonThiServiceClient w = new eTMS.Controllers.ThiSinhMonThiService.ThiSinhMonThiServiceClient();
            foreach (DataRow row in _tblThiSinhRemoved.Rows)
            {
                if (!row.IsNull("ThiSinhMonThi_ID"))
                {
                    w.DeleteByID(ConvertInt(row["ThiSinhMonThi_ID"]));
                }
            }
            _tblThiSinhRemoved.Rows.Clear();
        }
        public void SaveThiSinhMonThiRows(DataRow[] Rows)
        {
            ThiSinhMonThiService.ThiSinhMonThiServiceClient w = new eTMS.Controllers.ThiSinhMonThiService.ThiSinhMonThiServiceClient();
            List<DataRow> lsRowAddNew = new List<DataRow>();
            List<ThiSinhMonThiService.ThiSinhMonThiEntity> lsAddNew = new List<eTMS.Controllers.ThiSinhMonThiService.ThiSinhMonThiEntity>();
            List<ThiSinhMonThiService.ThiSinhMonThiEntity> lsUpdate = new List<eTMS.Controllers.ThiSinhMonThiService.ThiSinhMonThiEntity>();
            foreach (DataRow row in Rows)
            {
                ThiSinhMonThiService.ThiSinhMonThiEntity o = new eTMS.Controllers.ThiSinhMonThiService.ThiSinhMonThiEntity();
                if (row.IsNull("DiemThi"))
                {
                    o.DiemThi = 0;
                }
                else
                {
                    o.DiemThi = ConvertNumber(row["DiemThi"]);
                }
                o.MonThi_ID = MonThiObject.MonThi_ID;
                if (row.IsNull("PhongThi_ID"))
                {
                    o.PhongThi_ID = 0;
                }
                else
                {
                    o.PhongThi_ID = ConvertInt(row["PhongThi_ID"]);
                }
                o.SinhVien_ID = ConvertInt(row["SinhVien_ID"]);
                o.SoBaoDanh = row["SoBaoDanh"].ToString();
                if (!row.IsNull("TrangThai"))
                    o.TrangThai = ConvertByte(row["TrangThai"]);
                else
                    o.TrangThai = 0;
                if (!row.IsNull("ThiSinhMonThi_ID"))
                    o.ThiSinhMonThi_ID = ConvertInt(row["ThiSinhMonThi_ID"]);
                if (o.ThiSinhMonThi_ID == 0)
                {
                    lsAddNew.Add(o);
                    lsRowAddNew.Add(row);
                }
                else
                {
                    lsUpdate.Add(o);
                }
            }
            w.UpdateList(lsUpdate.ToArray());
            lsAddNew=new List<eTMS.Controllers.ThiSinhMonThiService.ThiSinhMonThiEntity>(w.SaveListRet(lsAddNew.ToArray()));
            for (int i = 0; i < lsAddNew.Count; i++)
            {
                lsRowAddNew[i]["ThiSinhMonThi_ID"] = lsAddNew[i].ThiSinhMonThi_ID;
            }
        }
        public void ChangePhongThiRows(DataRow[] Rows, PhongThiEntity phongThiEntity)
        {
            foreach (DataRow row in Rows)
            {
                row["PhongThi_ID"] = phongThiEntity.PhongThi_ID;
                row["MaPhongThi"] = phongThiEntity.MaPhongThi;
            }
        }
        public void LoadDiemMonHoc(int HocKy_ID, int NamHoc_ID, int MonHoc_ID, int LoaiDiem_ID)
        {

            if (!tblThiSinh.Columns.Contains("NotIsValid"))
            {
                tblThiSinh.Columns.Add("NotIsValid", typeof(bool));
            }
            if (!tblThiSinh.Columns.Contains("Diem"))
            {
                tblThiSinh.Columns.Add("Diem", typeof(decimal));
            }
            if (!tblThiSinh.Columns.Contains("SinhVienMonHoc_ID"))
            {
                tblThiSinh.Columns.Add("SinhVienMonHoc_ID", typeof(int));
            }
            if (!tblThiSinh.Columns.Contains("DiemMonHoc_ID"))
            {
                tblThiSinh.Columns.Add("DiemMonHoc_ID", typeof(int));
            }
            SinhVienMonHocService.SinhVienMonHocServiceClient sinhVienMonHocService = new eTMS.Controllers.SinhVienMonHocService.SinhVienMonHocServiceClient();
            List<int> lsIDSinhVien = new List<int>();
            List<DataRow> lsDuyet = new List<DataRow>();
            foreach (DataRow r in tblThiSinh.Rows)
            {
                r["NotIsValid"] = false;
                lsIDSinhVien.Add(ConvertInt(r["SinhVien_ID"]));
                lsDuyet.Add(r);
                if (lsIDSinhVien.Count >= 1000)
                {
                    DataTable tblTemp = sinhVienMonHocService.SelectAllDSWithDiemMonHoc(HocKy_ID, NamHoc_ID, LoaiDiem_ID, MonHoc_ID, lsIDSinhVien.ToArray()).Tables[0];
                    foreach (DataRow row in lsDuyet)
                    {
                        DataRow[] rs = tblTemp.Select("SinhVien_ID=" + row["SinhVien_ID"].ToString());
                        if (rs.Length > 0)
                        {
                            row["DiemMonHoc_ID"] = rs[0]["DiemMonHoc_ID"];
                            row["SinhVienMonHoc_ID"] = rs[0]["SinhVienMonHoc_ID"];
                            row["Diem"] = rs[0]["Diem"];
                            if (!rs[0].IsNull("Diem"))
                            {
                                r["NotIsValid"] = ConvertNumber(row["Diem"]) != ConvertNumber(row["DiemThi"]);
                            }
                        }
                    }
                    lsDuyet = new List<DataRow>();
                    lsIDSinhVien = new List<int>();
                }
            }
            if (lsIDSinhVien.Count > 0)
            {
                DataTable tblTemp = sinhVienMonHocService.SelectAllDSWithDiemMonHoc(HocKy_ID, NamHoc_ID, LoaiDiem_ID, MonHoc_ID, lsIDSinhVien.ToArray()).Tables[0];
                foreach (DataRow r in lsDuyet)
                {
                    DataRow[] rs = tblTemp.Select("SinhVien_ID=" + r["SinhVien_ID"].ToString());
                    if (rs.Length > 0)
                    {
                        r["DiemMonHoc_ID"] = rs[0]["DiemMonHoc_ID"];
                        r["SinhVienMonHoc_ID"] = rs[0]["SinhVienMonHoc_ID"];
                        r["Diem"] = rs[0]["Diem"];
                        if (!rs[0].IsNull("Diem"))
                        {
                            r["NotIsValid"] = ConvertNumber(r["Diem"]) != ConvertNumber(r["DiemThi"]);
                        }
                    }
                }
            }
        }
        public void UpdateToDiemMonHoc(int HocKy_ID, int NamHoc_ID, int MonHoc_ID, int LoaiDiem_ID,int LanThi)
        {
            SinhVienMonHocService.SinhVienMonHocServiceClient sinhVienMonHocService = new eTMS.Controllers.SinhVienMonHocService.SinhVienMonHocServiceClient();
            DiemMonHocService.DiemMonHocServiceClient diemMonHocService = new eTMS.Controllers.DiemMonHocService.DiemMonHocServiceClient();
            KhoaHocController khoaHocController = new KhoaHocController();
            foreach (DataRow r in tblThiSinh.Rows)
            {
                int sinhVienMonHoc_ID = ConvertInt(r["SinhVienMonHoc_ID"]);
                if (sinhVienMonHoc_ID == 0)
                {
                    SinhVienMonHocService.SinhVienMonHocEntity sinhVienMonHocEntity = new eTMS.Controllers.SinhVienMonHocService.SinhVienMonHocEntity();
                    sinhVienMonHocEntity.HocKy_ID = HocKy_ID;
                    sinhVienMonHocEntity.MonHoc_ID = MonHoc_ID;
                    sinhVienMonHocEntity.NamHoc_ID = NamHoc_ID;
                    sinhVienMonHocEntity.NamThu = 1;
                    sinhVienMonHocEntity.SinhVien_ID = ConvertInt(r["SinhVien_ID"]);
                    sinhVienMonHocEntity = sinhVienMonHocService.SaveRet(sinhVienMonHocEntity);
                    sinhVienMonHoc_ID = sinhVienMonHocEntity.SinhVienMonHoc_ID;
                }
                DiemMonHocService.DiemMonHocEntity oDiemMonHoc = new eTMS.Controllers.DiemMonHocService.DiemMonHocEntity();
                oDiemMonHoc.Diem = ConvertNumber(r["DiemThi"]);
                oDiemMonHoc.DiemMonHoc_ID = ConvertInt(r["DiemMonHoc_ID"]);
                oDiemMonHoc.SinhVienMonHoc_ID = sinhVienMonHoc_ID;
                oDiemMonHoc.MonHoc_ID = MonHoc_ID;
                oDiemMonHoc.LoaiDiem_ID = LoaiDiem_ID;
                oDiemMonHoc.LanThi = (byte)LanThi;
                if (oDiemMonHoc.DiemMonHoc_ID > 0)
                {
                    diemMonHocService.Update(oDiemMonHoc);
                }
                else
                {
                    diemMonHocService.Save(oDiemMonHoc);
                }
            }
        }
    }
}
